﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Site_ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		// populate the control only on the initial page load
		if (!IsPostBack)
		{
			this.Form.Action = "Site/ShoppingCart.aspx";
			PopulateControls();
		}
    }

	// fill shopping cart controls with data
	private void PopulateControls()
	{
		// get the items in the shopping cart
		DataTable dt = ShoppingCartAccess.GetItems();
		// if the shopping cart is empty...
		if (dt.Rows.Count == 0)
		{
			ReportLabel.Text = "លោកអ្នកមិនមានទំនិញក្នុងកន្ត្រកទេ!";
			CartView.Visible = false;
			checkoutButton.Enabled = false;
			updateButton.Enabled = false;
			totalAmountLabel.Text = String.Format("{0:c}", 0);
		}
		else
		// if the shopping cart is not empty...
		{
			// populate the list with the shopping cart contents
			CartView.DataSource = dt;
			CartView.DataBind();
			// setup control
			ReportLabel.Text = "លោកអ្នកមិនមានទំនិញក្នុងកន្ត្រកទេ!";
			CartView.Visible = true;
			updateButton.Enabled = true;
			checkoutButton.Enabled = true;
			// display the total amount
			decimal amount = ShoppingCartAccess.GetTotalAmount();
			totalAmountLabel.Text = String.Format("{0:c}", amount);
		}
	}

	// remove a product from the cart
	protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		// Index of the row being deleted
		int rowIndex = e.RowIndex;
		// The ID of the product being deleted
		string productId = CartView.DataKeys[rowIndex].Value.ToString();
		// Remove the product from the shopping cart
		bool success = ShoppingCartAccess.RemoveItem(productId);
		// Display status
		statusLabel.Text = success ? "Product successfully removed!" :
					  "There was an error removing the product! ";
		// Repopulate the control
		PopulateControls();
		Response.Redirect(Request.RawUrl);
	}

	// update shopping cart product quantities
	protected void updateButton_Click(object sender, EventArgs e)
	{
		// Number of rows in the GridView
		int rowsCount = CartView.Rows.Count;
		// Will store a row of the GridView
		GridViewRow gridRow;
		// Will reference a quantity TextBox in the GridView
		TextBox quantityTextBox;
		// Variables to store product ID and quantity
		string productId;
		int quantity;
		// Was the update successful?
		bool success = true;
		// Go through the rows of the GridView
		for (int i = 0; i < rowsCount; i++)
		{
			// Get a row
			gridRow = CartView.Rows[i];
			// The ID of the product being deleted
			productId = CartView.DataKeys[i].Value.ToString();
			// Get the quantity TextBox in the Row
			quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
			// Get the quantity, guarding against bogus values
			if (Int32.TryParse(quantityTextBox.Text, out quantity))
			{
				// Update product quantity
				success = success && ShoppingCartAccess.UpdateItem(productId, quantity);
			}
			else
			{
				// if TryParse didn't succeed
				success = false;
			}

			// Display status message
			statusLabel.Text = success ?
			"Your shopping cart was successfully updated!" :
			"Some quantity updates failed! Please verify your cart!";
		}
		// Repopulate the control
		PopulateControls();
	}
}